package com.example.sources.jdbc.proxy;

import com.example.sources.jdbc.tool.RegexUtil;
import com.example.sources.jdbc.tool.TypeUtil;

import java.lang.reflect.Field;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/9/19.
 */
public class SqlParameterStatement {
    private final PreparedStatement preparedStatement;
    private Statement statement;

    public SqlParameterStatement(Connection connection,String query) throws SQLException {
        statement = connection.createStatement();
        this.preparedStatement = connection.prepareStatement(query);
    }

    public void setQuery(String[] paramsType,Object[] params) throws SQLException {
        int index = 1;
        for (String type:paramsType){
            if (type.contains("String")){
                setString(index,params[index-1]);
            }else if (type.contains("int")){
                setInt(index,params[index-1]);
            }else if (type.contains("Long")){
                setLong(index,params[index-1]);
            }else {
                setObj(index,params[index-1]);
            }
            index++;
        }

    }

    public Object returnConvert(ResultSet resultSet,String returnType) {
        Object obj = null;

        try {
            if (TypeUtil.isBaseType(returnType)){
                while (resultSet.next()){
                    obj = resultSet.getObject(1);
                }
            }else if (TypeUtil.isIterable(returnType.split("<")[0])){
                List<String> beanNameList = RegexUtil.matchSpecificString(returnType, "<(.*?)>");
                String beanName;
                if (beanNameList.size() == 0){
                    beanName = "java.lang.Object";
                }else {
                    beanName = beanNameList.get(0);
                }


                List collection = new ArrayList<>();

                if (TypeUtil.isBaseType(beanName)){
                    while (resultSet.next()){
                        Class c = Class.forName(beanName);
                        Object o = resultSet.getObject(1);
                        collection.add(o);
                    }
                    obj = collection;
                }else {



                ResultSetMetaData rsmd = resultSet.getMetaData();
                int colCount = rsmd.getColumnCount();

                while (resultSet.next()){
                    Class c = Class.forName(beanName);
                    Object o = c.newInstance();
                    for(int i = 1;i<=colCount;i++){
                        Field field = c.getDeclaredField(rsmd.getColumnLabel(i));
                        field.setAccessible(true);
                        field.set(o,resultSet.getObject(rsmd.getColumnLabel(i),Class.forName(rsmd.getColumnClassName(i))));
                    }
                    collection.add(o);
                }
                obj = collection;
                }
            }else {
                ResultSetMetaData rsmd = resultSet.getMetaData();
                int colCount = rsmd.getColumnCount();
                while (resultSet.next()){
                    Class c = Class.forName(returnType);
                    Object o = c.newInstance();
                    for(int i = 1;i<=colCount;i++){
                        Field field = c.getDeclaredField(rsmd.getColumnLabel(i));
                        field.setAccessible(true);
                        field.set(o,resultSet.getObject(rsmd.getColumnLabel(i),Class.forName(rsmd.getColumnClassName(i))));
                    }
                    obj = o;
                }
            }


        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return obj;
    }



    public ResultSet excute() throws SQLException {
        return preparedStatement.executeQuery();
    }

    public void setInt(int index,Object o) throws SQLException {
        preparedStatement.setInt(index, (Integer) o);
    }

    public void setLong(int index,Object o) throws SQLException {
        preparedStatement.setLong(index, (Long) o);
    }

    public void setString(int index,Object o) throws SQLException {
        preparedStatement.setString(index, (String) o);
    }

    public void setObj(int index,Object o) throws SQLException {
        preparedStatement.setObject(index,o);
    }

    public void close() throws SQLException {
        statement.close();
        preparedStatement.close();
    }

}
